﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ListViewItemComparer.cs" company="">
//   
// </copyright>
// <summary>
//   Defines the ListViewItemComparer type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ComparisonTool.Connection
{
    using System.Collections;
    using System.Windows.Forms;

    /// <summary>
    /// The list view item comparer.
    /// </summary>
    public class ListViewItemComparer : IComparer
    {
        /// <summary>
        /// The col.
        /// </summary>
        private int col;

        /// <summary>
        /// The inner order.
        /// </summary>
        private SortOrder innerOrder;

        /// <summary>
        /// Initializes a new instance of the <see cref="ListViewItemComparer"/> class.
        /// </summary>
        public ListViewItemComparer()
        {
            this.col = 0;
            this.innerOrder = SortOrder.Ascending;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ListViewItemComparer"/> class.
        /// </summary>
        /// <param name="column">
        /// The column.
        /// </param>
        /// <param name="order">
        /// The order.
        /// </param>
        public ListViewItemComparer(int column, SortOrder order)
        {
            this.col = column;
            this.innerOrder = order;
        }

        /// <summary>
        /// The compare.
        /// </summary>
        /// <param name="x">
        /// The x.
        /// </param>
        /// <param name="y">
        /// The y.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int Compare(object x, object y)
        {
            return this.Compare((ListViewItem)x, (ListViewItem)y);
        }

        /// <summary>
        /// The compare.
        /// </summary>
        /// <param name="x">
        /// The x.
        /// </param>
        /// <param name="y">
        /// The y.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int Compare(ListViewItem x, ListViewItem y)
        {
            if (this.innerOrder == SortOrder.Ascending)
            {
                return string.CompareOrdinal(x.SubItems[this.col].Text, y.SubItems[this.col].Text);
            }

            return string.CompareOrdinal(y.SubItems[this.col].Text, x.SubItems[this.col].Text);
        }
    }
}
